package com.itheima.service.mongo.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.itheima.domain.mongo.Friend;
import com.itheima.domain.mongo.FriendMovement;
import com.itheima.domain.mongo.MyMovement;
import com.itheima.domain.vo.PageBeanVo;
import com.itheima.service.mongo.FriendService;
import com.itheima.util.ConstantUtil;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.List;


@DubboService
public class FriendServiceImpl implements FriendService {

    @Autowired
    private MongoTemplate mongoTemplate;

    //添加对方为好友
    @Override
    public void addFriend(Long userId, Long friendId) {
        //先查一下friend表中是否有我们的朋友关系，有就不添加了
        Query query = new Query(Criteria.where("userId").is(userId).and("friendId").is(friendId));
        boolean exists = mongoTemplate.exists(query, Friend.class);
        if (!exists){
            //添加好友
            Friend friend = new Friend();
            friend.setCreated(System.currentTimeMillis());
            friend.setUserId(userId);
            friend.setFriendId(friendId);
            mongoTemplate.save(friend);
            //推送我的动态给好友看
            List<MyMovement> myMovements = mongoTemplate.findAll(MyMovement.class, ConstantUtil.MOVEMENT_MINE + userId);

            if (CollectionUtil.isNotEmpty(myMovements)) {
                for (MyMovement myMovement : myMovements) {
                    FriendMovement friendMovement = new FriendMovement();
                    friendMovement.setCreated(myMovement.getCreated());
                    friendMovement.setUserId(userId);
                    friendMovement.setPublishId(myMovement.getPublishId());
                    mongoTemplate.save(friendMovement, ConstantUtil.MOVEMENT_FRIEND+friendId);
                }
            }
        }

        //查一下好友是否和我是朋友关系
        Query queryFriend = new Query(Criteria.where("userId").is(friendId).and("friendId").is(userId));
        boolean existsF = mongoTemplate.exists(queryFriend, Friend.class);
        if (!existsF){
            //添加好友关系
            Friend friend = new Friend();
            friend.setCreated(System.currentTimeMillis());
            friend.setUserId(friendId);
            friend.setFriendId(userId);
            mongoTemplate.save(friend);
            //推送对方的动态给我
            List<MyMovement> friendMovements = mongoTemplate.findAll(MyMovement.class, ConstantUtil.MOVEMENT_MINE + friendId);

            if (CollectionUtil.isNotEmpty(friendMovements)) {
                for (MyMovement friendMovement : friendMovements) {
                    FriendMovement myFriendMovement = new FriendMovement();
                    myFriendMovement.setPublishId(friendMovement.getPublishId());
                    myFriendMovement.setUserId(friendId);
                    myFriendMovement.setCreated(friendMovement.getCreated());
                    mongoTemplate.save(myFriendMovement, ConstantUtil.MOVEMENT_FRIEND+userId);
                }
            }
        }

    }

    //查询好友列表
    @Override
    public PageBeanVo findContacts(Long userId, Integer pageNum, Integer pageSize) {
        Integer index = (pageNum - 1) * pageSize;
        Query query = new Query(Criteria.where("userId").is(userId))
                .with(Sort.by(Sort.Order.desc("created")))
                .skip(index).limit(pageSize);
        List<Friend> friends = mongoTemplate.find(query, Friend.class);

        long count = mongoTemplate.count(query, Friend.class);
        return new PageBeanVo(pageNum,pageSize,count,friends);
    }

    //删除好友关系
    @Override
    public void removeFriend(Long userId, Long friendId) {
        //我和他是好友关系，动态
        mongoTemplate.remove(new Query(Criteria.where("userId").is(userId).and("friendId").is(friendId)), Friend.class);
        mongoTemplate.remove(new Query(Criteria.where("userId").is(friendId)), ConstantUtil.MOVEMENT_FRIEND+userId);

        //他和我是好友关系，动态
        mongoTemplate.remove(new Query(Criteria.where("userId").is(friendId).and("friendId").is(userId)), Friend.class);
        mongoTemplate.remove(new Query(Criteria.where("userId").is(userId)), ConstantUtil.MOVEMENT_FRIEND+friendId);
    }
}
